﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DefeatEpidemic.Common;
using DefeatEpidemic.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DefeatEpidemic.Controllers
{
    public class ManagerController : Controller
    {
        private readonly DefeatEpidemicContext _context;

        public ManagerController(DefeatEpidemicContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Login()
        {
            HttpContext.DeleteCookies("username");
            //HttpContext.Session.Remove(Constants.USERID);
            HttpContext.Session.Clear();
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Login(RequestModels.LoginModel model)
        {
            Response resp = new Response();
            if (ModelState.IsValid)
            {
                string pwd = Security.SHA256(model.Password);
                if (_context.Managers.Count(u => u.name == model.UserName && u.password== pwd) == 1)
                {
                    resp.code = 1;
                    resp.message = "login success";
                    HttpContext.Session.SetString(Constants.USERID, model.UserName);
                    HttpContext.SetCookies("username", model.UserName, 60);                    
                }
            }
            return Json(resp);
        }
    }
}